package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import com.itheima.health.utils.myutil.MyBeanUtil;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SuppressWarnings("ALL")
/**
 * @Description :
 * @author     :LD
 */
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    //将excel表格导入数据库
    @Override
    public void importOrderSettings(List<String[]> orderSettings){
        List<OrderSetting> orderSettingsList = null;
        try {
            orderSettingsList = MyBeanUtil.ObjectToBeanList(orderSettings, OrderSetting.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (OrderSetting orderSetting : orderSettingsList) {
            //判断数据库中是否已存在该日期数据，若存在则更新，否则添加
            QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("orderdate", orderSetting.getOrderDate());
            OrderSetting order = baseMapper.selectOne(queryWrapper);
            if (order != null) {
                orderSetting.setId(order.getId());
            }
        }
        //批量添加或更新
        saveOrUpdateBatch(orderSettingsList);
    }

    @SneakyThrows
    @Override
    public Map findSettingData(int year, int month) {
        String beginDate = year+"-"+month+"-1";
        String endDate = DateUtils.parseDate2String(DateUtils.getLastDayOfYearAndMonth(year,month));
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("orderdate",beginDate,endDate);
        List<OrderSetting> list = list(queryWrapper);
        //  Map   前后数据交互 格式 对象
        Map<String,Object> map  = new HashMap();
        for (OrderSetting orderSetting : list) {
            Date orderDate = orderSetting.getOrderDate();
            String dateString = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
            //   需要的 数据 传递 前端   灵活 方便    List  Map    json
            Map  orderSettingMap = new HashMap();
            orderSettingMap.put("number",orderSetting.getNumber());
            orderSettingMap.put("reservations",orderSetting.getReservations());
            map.put(dateString,orderSettingMap);
        }
        return map;
    }

    @Override
    public Boolean updateNumberByOrderdate(int number, String orderdate) {
        return baseMapper.updateNumberByOrderdate(number,orderdate);
    }

    @Override
    @LcnTransaction
    public int isOrderOk(String orderDate) {
        return baseMapper.isOrderOk(orderDate);
    }

    @Override
    @LcnTransaction
    public void updateReservationByOrderDate(String orderDate) {
        baseMapper.updateReservationsByOrderDate(orderDate);
    }

    @Override
    public void deleteExpiredOrders() {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("orderdate",new Date());
        remove(queryWrapper);
    }
}
